#ifndef __LV_CUSTOM_LIST_H_
#define __LV_CUSTOM_LIST_H_

#ifdef __cplusplus
extern "C" { //only need to export C interface if
    // used by C++ source code
#endif

struct list_head
{
    struct list_head* next;
    struct list_head* prev;
};


#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head* list)
{
    list->next = list;
    list->prev = list;
}

/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_add(struct list_head* node,
    struct list_head* prev,
    struct list_head* next)
{
    next->prev = node;
    node->next = next;
    node->prev = prev;
    prev->next = node;
}

static inline void list_add(struct list_head* node, struct list_head* head)
{
    __list_add(node, head, head->next);
}

static inline void list_add_tail(struct list_head* node, struct list_head* head)
{
    __list_add(node, head->prev, head);
}

static inline void __list_del(struct list_head* prev, struct list_head* next)
{
    next->prev = prev;
    prev->next = next;
}

static inline void list_move(struct list_head* list, struct list_head* head)
{
    __list_del(list->prev, list->next);
    list_add(list, head);
}

static inline void list_move_tail(struct list_head* list, struct list_head* head)
{
    __list_del(list->prev, list->next);
    list_add_tail(list, head);
}

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:    the type of the container struct this is embedded in.
 * @member:    the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) \
     ((type *)( (char*)(ptr) - offsetof(type,member) ))

/**
 * list_entry - get the struct for this entry
 * @ptr:    the &struct list_head pointer.
 * @type:    the type of the struct this is embedded in.
 * @member:    the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:    the type of the container struct this is embedded in.
 * @member:    the name of the member within the struct.
 *
 */
#define list_for_each_entry(pos, head, member, type)    \
     for (pos = list_entry((head)->next, type, member); \
          &(pos->member) != (head);     \
          pos = list_entry(pos->member.next, type, member))

static inline int list_empty(const struct list_head* head)
{
    return head->next == head;
}

static inline int list_empty_careful(const struct list_head* head)
{
    struct list_head* next = head->next;
    return (next == head) && (next == head->prev);
}

#ifdef __cplusplus
}
#endif

#endif // !__LIST_H_

